package deviceinfo.wsm.com.deviceinfo.plugin.com.an.deviceinfo.permission;


import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.content.ContextCompat;

public class PermissionUtils {

    private Context context;

    public PermissionUtils(Context context) {
        this.context = context;
    }

    public synchronized final boolean isPermissionGranted(String[] permission) {
        boolean result = true;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            for (String aPermission : permission) {
                if (!(context.checkCallingOrSelfPermission(aPermission) == PackageManager.PERMISSION_GRANTED)) {
                    result = false;
                }
            }
            return result;
        }

        for (String s : permission) {
            if (!(ContextCompat.checkSelfPermission(context, s) == PackageManager.PERMISSION_GRANTED)) {
                result = false;
            }
        }
        return result;
    }

    public void openAppSettings() {
        final Intent i = new Intent();
        i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        i.addCategory(Intent.CATEGORY_DEFAULT);
        i.setData(Uri.parse("package:" + context.getPackageName()));
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        context.startActivity(i);
    }
}